package com.letv.android.client.episode.parser;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Iterator;
import java.util.LinkedHashMap;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.text.TextUtils;

import com.letv.core.bean.AlbumNewListByDateBean;
import com.letv.core.bean.VideoBean;
import com.letv.core.parser.LetvMobileParser;
import com.letv.core.utils.LogInfo;

/**
 * 专辑，视频列表(按年月获取)解析
 * */
public class AlbumNewListByDateParser extends LetvMobileParser<AlbumNewListByDateBean> {
	
	@Override
	public AlbumNewListByDateBean parse(JSONObject data) throws JSONException {

        try {
        		if (has(data, "videoList")) {
        			data = getJSONObject(data, "videoList");
        		}
                VideoParser parse = new VideoParser();
                AlbumNewListByDateBean albumNewListByDatePlayerLibs = new AlbumNewListByDateBean();
                Iterator<String> iters = data.keys();
                ArrayList<String> newList = setNewDate(new ArrayList<String>() , iters);
                LinkedHashMap<String , AlbumNewListByDateBean.MonthList> hashMap = new LinkedHashMap<String ,AlbumNewListByDateBean.MonthList>();
                for(int i = 0 ; i < newList.size() ; i++) {
                    String key = newList.get(i);
                    LogInfo.log("king","year key = " + key);
                    JSONObject monthValue = getJSONObject(data, key);
                    AlbumNewListByDateBean.MonthList monthList = null;
                    if (monthValue!=null) {
                            monthList = new AlbumNewListByDateBean.MonthList();
                            Iterator<String> monthIter = monthValue.keys();
                            ArrayList<String> newListMonth = setNewDate(new ArrayList<String>() , monthIter);
                            for(int j = 0 ; j < newListMonth.size() ; j++){
                                String monthKey = newListMonth.get(j);
                                LogInfo.log("king","month key = " + monthKey);
                                JSONArray  monthArray = getJSONArray(monthValue, monthKey);

                                ArrayList<VideoBean> list = new ArrayList<VideoBean>();
                                if (monthArray!=null&&monthArray.length()>0) {
                                    for (int z = 0 ;z<monthArray.length();z++){
                                        JSONObject obj = monthArray.getJSONObject(z);
                                        list.add(parse.parse(obj));
                                    }
                                }
                                monthList.put(monthKey,list);
                            }
                    }
                    hashMap.put(key,(AlbumNewListByDateBean.MonthList)changeHashMap(monthList,null));
                }
                albumNewListByDatePlayerLibs.setYearList((LinkedHashMap<String , AlbumNewListByDateBean.MonthList>)changeHashMap(null,hashMap));
                return albumNewListByDatePlayerLibs;
        }catch (Exception e ){
            e.printStackTrace();
        }
        return null;
    }

    private ArrayList<String> setNewDate(ArrayList<String> list, Iterator<String> iter) {
    	 while (iter.hasNext()) {
             String key = iter.next();
             if (TextUtils.isDigitsOnly(key)) {
            	 list.add(key);
             }
    	 }
    	 Collections.sort(list, new MyComparator());
    	 return list;
	}

	public Object changeHashMap(AlbumNewListByDateBean.MonthList monthMap,LinkedHashMap<String , AlbumNewListByDateBean.MonthList> yearMap){
        int length = 0;
        if(monthMap!=null){
            length = monthMap.size();
        }
        if(yearMap!=null){
            length = yearMap.size();
        }
        String keys[] = new String[length];
        Object objs[] = new Object[length];
        AlbumNewListByDateBean.MonthList tempMonthMap = new AlbumNewListByDateBean.MonthList();
        LinkedHashMap<String,AlbumNewListByDateBean.MonthList> tempYearMap = new LinkedHashMap<String, AlbumNewListByDateBean.MonthList>();
        Iterator<String> mapKey = null;
        if(monthMap!=null){
            mapKey = monthMap.keySet().iterator();
        }
        if(yearMap!=null){
            mapKey = yearMap.keySet().iterator();
        }
        int i = 0;
        while (mapKey.hasNext()){
            String key  = mapKey.next();
            keys[i] = key;
            if(monthMap!=null){
                objs[i] = monthMap.get(key);
            }
            if(yearMap!=null){
                objs[i] = yearMap.get(key);
            }
            i++;
        }

        if(monthMap!=null){
            for(i=length-1;i>=0;i--){
                tempMonthMap.put(keys[i],(ArrayList<VideoBean>)objs[i]);
            }
            return tempMonthMap;
        }
        if(yearMap!=null){
            for(i=length-1;i>=0;i--){
                tempYearMap.put(keys[i],(AlbumNewListByDateBean.MonthList)objs[i]);
            }
            return tempYearMap;
        }
        return null;
    }
    
    class MyComparator implements Comparator<String>{
	    public int compare(String arg0, String arg1){
		    int s1 = Integer.parseInt(arg0);
		    int s2 = Integer.parseInt(arg1);
		    if(s1 > s2){
		    	return 1;
		    }else if(s1 == s2){
		    	return 0;
		    }else{
		    	return -1;
		    }
	    }
    }
}
